
# coding: utf-8

# In[1]:

import lstm
import time
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


# In[2]:

#Main Run Thread
if __name__=='__main__':
    global_start_time = time.time()

    print('> Loading data... ')

    #X_train, y_train, X_test, y_test = lstm.load_data('sp500.csv', seq_len, True)
    IDIR = '../data/'
    print('loading file')
    x = np.arange(-50.0, 50.0, 1)
    y1 = np.sin(x)
    y1 = np.array(y1)


# In[18]:

def load_data(data, seq_len, normalise_window):
    sequence_length = seq_len + 1
    result = []
    for index in range(len(data) - sequence_length):
        result.append(data[index: index + sequence_length])
    
    if normalise_window:
        result = normalise_windows(result)

    result = np.array(result)

    row = round(0.9 * result.shape[0])
    train = result[:int(row), :]
    np.random.shuffle(train)
    x_train = train[:, :-1]
    y_train = train[:, -1]
    x_test = result[int(row):, :-1]
    y_test = result[int(row):, -1]

    x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
    x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))  

    return [x_train, y_train, x_test, y_test]
x_train,y_train,x_test,y_test = load_data(y1,5,0)


# In[20]:

epochs  = 100
def plot_results(predicted_data, true_data):
    fig = plt.figure(facecolor='white')
    ax = fig.add_subplot(111)
    ax.plot(true_data, label='True Data')
    plt.plot(predicted_data, label='Prediction')
    plt.legend()
    plt.show()
model = lstm.build_model([1, 5, 20, 1])
model.fit(
            x_train,
            y_train,
            batch_size=512,
            nb_epoch=epochs,
            validation_split=0.05)
#predictions = lstm.predict_sequences_multiple(model, X_test, seq_len, 50)
#predicted = lstm.predict_sequence_full(model, X_test, seq_len)
predicted = lstm.predict_point_by_point(model, x_test)
plot_results(predicted, y_test)